/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Producer.h"
#include "Manager.h"

#include <mediator/Request.h>
#include <gfx/Interface.h>

#include <OgreRoot.h>
#include <OgreRenderWindow.h>

namespace galaxy {
    namespace gfx {
        namespace ogre3d {

            Producer::Producer(Manager& manager)
            {
                m_Manager = &manager;
            }

            Producer::~Producer(void)
            {
            }

            bool Producer::handleRequest(const mediator::Request& Request, mediator::RequestResultPtr& result) {
                result.reset();
                switch (Request.type()) {
                    case Request_TYPE_GFX_WINDOW_HANDLE:
                        {
                            return RequestWindowHandle (result);
                        }
                }
                return (const mediator::RequestResult *) 0L;
            }

            bool Producer::RequestWindowHandle (mediator::RequestResultPtr& result) {
                Ogre::Root * root = m_Manager->getRoot();
                if (root) {
                    size_t windowHnd = 0;
                    std::ostringstream windowHndStr;
                    Ogre::RenderWindow *win = root->getAutoCreatedWindow();
                    win->getCustomAttribute("WINDOW", &windowHnd);
                    windowHndStr << windowHnd;

                    GfxRequestWinInfo * q = new GfxRequestWinInfo();
                    q->windowHandle = windowHndStr.str();
                    win->getMetrics(q->width, q->height, q->depth, q->left, q->top);

                    result.reset (q);

                    return true;
                }
                return false;
            }

        } // namespace ogre3d
    } // namespace gfx
} // namespace galaxy
